package demo.controller.adm;

import demo.dao.FileDao;
import demo.dao.UserDao;
import demo.entity.LargeFile;
import demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/teaadm")
public class TeaAdmController {
    @Autowired(required = false)
    UserDao userDao;
    @Autowired(required = false)
    FileDao fileDao;
    @RequestMapping("/show")
    public String show(Model model, HttpSession session){
        List<User> all = userDao.findByRole("tea");
        User user=new User();
        model.addAttribute("user",user);
        model.addAttribute("list",all);
        model.addAttribute("action","add");
        return "/adm/tea";
    }
    @RequestMapping("/go2upte/{uid}")
    public String go2upTe(Model model,@PathVariable("uid") Integer uid){
        User user=userDao.findByUid(uid);
        model.addAttribute("user",user);
        model.addAttribute("src","src");
        model.addAttribute("action","upte");
        return "forward:/teaadm/showup";
    }
    @PostMapping("/upte")
    public String upTe(User user, MultipartFile img, Model model){
        User user1 = userDao.findByUid(user.getUid());
        if (img==null) {
            user.setPic(user1.getPic());
            userDao.update(user);
            model.addAttribute("addUp","更改成功");
            return "forward:/teaadm/show";
        }
        fileDao.delete(user1.getPic());
        String string = UUID.randomUUID().toString();
        string = string.replace("-", "");//将字符串的内容替换掉
        //保存图片
        try {
            LargeFile largeFile = new LargeFile();
            largeFile.setId(string);
            largeFile.setFilename(img.getOriginalFilename());//文件文件名String
            byte[] bytes = img.getBytes();
            largeFile.setContent(bytes);//文件内容Object
            fileDao.add(largeFile);
        }catch (Exception a){
            model.addAttribute("addUp","更改失败");
            return "forward:/teaadm/show";
        }
        //更改
        user.setPic(string);
        userDao.update(user);
        model.addAttribute("addUp","更改成功");
        return "forward:/teaadm/show";
    }
    @PostMapping("/add")
    public String add(MultipartFile img,User user,Model model){
        //添加
        if (img==null){
            model.addAttribute("addUp","添加失败");
            return "forward:/teaadm/show";
        }
        String string = UUID.randomUUID().toString();
        string = string.replace("-", "");//将字符串的内容替换掉
        try {
            LargeFile largeFile = new LargeFile();
            largeFile.setId(string);
            largeFile.setFilename(img.getOriginalFilename());//文件文件名String
            byte[] bytes = img.getBytes();
            largeFile.setContent(bytes);//文件内容Object
            fileDao.add(largeFile);
        }catch (Exception a){
            model.addAttribute("addUp","添加失败");
            return "forward:/teaadm/show";
        }
        user.setRole("tea");
        user.setPic(string);
        userDao.add(user);
        System.out.println(user);
        model.addAttribute("addUp","添加成功");
        return "forward:/teaadm/show";
    }
    @RequestMapping("/showup")
    public String shows(Model model,HttpSession session){
        User dl =(User) session.getAttribute("DL");
        if (dl==null)return "forward:/home/go2login";
        List<User> all = userDao.findByRole("tea");
        model.addAttribute("list",all);
        return "/adm/tea";
    }
    @RequestMapping("go2add")
    public String go2add(){
        return "forward:/teaadm/show";
    }
    @RequestMapping("/del/{uid}")
    public String del(@PathVariable("uid") Integer uid,Model model){
        User uids = userDao.findByUid(uid);
        userDao.del(uid);
        fileDao.delete(uids.getPic());
        model.addAttribute("addUp","删除成功");
        return "forward:/teaadm/show";
    }
}
